package com.blb.mvc.util;

import com.blb.mvc.entity.Student;
import org.springframework.core.convert.converter.Converter;

/**
 * 自定义转换器
 */
public class StudentConvertor implements Converter<String, Student> {

    @Override
    public Student convert(String s) {
        //id-name-age-address-subject --> Student
        if(s == null || !s.contains("-")){
            System.out.println("字符串格式错误");
            return null;
        }
        String[] strings = s.split("\\-");
        if(strings.length < 5){
            System.out.println("字符串格式错误");
            return null;
        }
        Student student = new Student();
        student.setId(Long.valueOf(strings[0]));
        student.setName(strings[1]);
        student.setAge(Integer.valueOf(strings[2]));
        student.setAddress(strings[3]);
        student.setSubject(strings[4]);
        return student;
    }
}
